package love.yuanmengrobot.springmvcdemo2.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;

import love.yuanmengrobot.springmvcdemo2.pojo.SysLoginfo;
import love.yuanmengrobot.springmvcdemo2.pojo.SysUser;
import love.yuanmengrobot.springmvcdemo2.service.LoginfoService;
import love.yuanmengrobot.springmvcdemo2.service.UserService;
import love.yuanmengrobot.springmvcdemo2.utils.RandomUtils;
import love.yuanmengrobot.springmvcdemo2.utils.ResultObj;
import love.yuanmengrobot.springmvcdemo2.utils.WebUtils;
import love.yuanmengrobot.springmvcdemo2.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    LoginfoService loginfoService;
    @Autowired
    UserService userService;

    @RequestMapping("/login")
    public ResultObj login(UserVo userVo) {

        userVo.setLoginname("1515");
        userVo.setPwd("123456");
        SysUser user = this.userService.login(userVo);
//        System.out.println(user.getLoginname());
        String code = WebUtils.getHttpSession().getAttribute("code").toString();
        if (!userVo.getCode().equals(code)) {
            if (null != user) {
                WebUtils.getHttpSession().setAttribute("user", user);

                SysLoginfo loginfo = new SysLoginfo();
                loginfo.setLogintime(RandomUtils.getTime());
//                loginfo.setLoginip(WebUtils.getHttpServletRequest().getLocalAddr());

                loginfo.setLoginip(getRemortIP(WebUtils.getHttpServletRequest()));
                loginfo.setLoginname(user.getRealname() + "-" + user.getLoginname());
                this.loginfoService.addLoginfo(loginfo);
                return ResultObj.LOGIN_SUCCESS;
            } else {
                return ResultObj.LOGIN_ERROR;
            }
        } else {
            return ResultObj.LOGIN_CODE_ERROR;
        }

    }

    /**
     * 获取验证码 放在session
     *
     * @param response
     * @param session
     * @throws IOException
     */
    @RequestMapping("/getCode")
    public void getCode(HttpServletResponse response, HttpSession session) throws IOException {
        //定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36, 4, 5);
        session.setAttribute("code", lineCaptcha.getCode());
        ServletOutputStream outputStream = response.getOutputStream();
        ImageIO.write(lineCaptcha.getImage(), "JPEG", outputStream);
    }


    public String getRemortIP(HttpServletRequest request) {
        if (request.getHeader("x-forwarded-for") == null) {
            return request.getRemoteAddr();
        }
        return request.getHeader("x-forwarded-for");
    }
}
